<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="kh">
        <el-input
          v-model="queryParams.kh"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总部编号" prop="dz">
        <el-input
          v-model="queryParams.zbbh"
          placeholder="请输入总部编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否已下场" prop="sfyxc" label-width="90px">
        <el-checkbox-group v-model="sfyxc" @change="selectSfyxc">
          <el-checkbox v-for="item in xcList" :label="item.dictValue">{{item.dictLabel}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="下场时间">
        <el-date-picker
          v-model="querySj"
          @change="selectTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          :default-time="['08:00:00', '08:00:00']"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-row style="margin-bottom: 15px">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gd:ywd:export']"
        >全部导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleSelectExport"
          v-hasPermi="['gd:ywd:export']"
        >选择导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ywdList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed label="总部编号" align="left" prop="zbbh">
        <template slot-scope="scope">
          <div style="color: #00afff;cursor: pointer;">
            <span @click="showInfo(scope.row)"> {{scope.row.zbbh}}</span> &nbsp;
            <el-link :underline="false" type="success" @click="copyText(scope.row.zbbh)" icon="el-icon-copy-document" style="font-size: 10px"></el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column fixed label="客户" align="left" prop="kh" show-overflow-tooltip />
      <el-table-column fixed label="地址" align="left" prop="dz" show-overflow-tooltip />
      <el-table-column label="检测方式" align="left" prop="jcfs" >
        <template slot-scope="scope">
          <span v-if="scope.row.jcfs==='2'">送检</span>
          <span v-else>下场</span>
        </template>
      </el-table-column>
      <el-table-column label="下场时间" align="center" prop="xcsj" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.xcsj) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下场状态" align="center" prop="sjzt">
        <template slot-scope="scope">
          <span v-if="scope.row.sfyxc==='1'"><el-tag type="warning">待安排</el-tag></span>
          <span v-else-if="scope.row.sfyxc==='2'"><el-tag type="warning">已安排待下场</el-tag></span>
          <span v-else-if="scope.row.sfyxc==='3'"><el-tag type="success">已下场</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="送检状态" align="center" prop="sfyxc">
        <template slot-scope="scope">
          <span v-if="scope.row.sjzt==='1'"><el-tag type="warning">已送检</el-tag></span>
          <span v-else-if="scope.row.sjzt==='2'"><el-tag type="warning">已送回</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="下场负责人" align="left" prop="xcfzrname"/>
      <el-table-column label="下场人员" align="left" prop="xcryname"/>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.jcfs === '1'"
            size="mini"
            type="text"
            icon="el-icon-s-custom"
            @click="apxcUser(scope.row)"
          >下场</el-button>
          <el-button
            v-if="scope.row.jcfs === '1'"
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="finalLog(scope.row)"
          >下场日志</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改业务单对话框 -->
    <form-info v-if="open" :showForm="open" :title="title" :formInfo="form" :ShowBtn="ShowBtn" @close="cancel"></form-info>

    <!-- 选择人员 -->
    <xcForm
      v-if="showUserDialog"
      :showSelectUser="showUserDialog"
      :form="xcData"
      @close="closeUser"
      @selectUser="handleUserSelection"
    ></xcForm>

    <!-- 下场人员日志 -->
    <XcLogList v-if="showXcLog" :showXcLog="showXcLog" :logData="logData" @closeXcLog="closeXcLog"></XcLogList>

  </div>
</template>

<script>
import FormInfo from '@/views/gd/xcap/formInfo.vue'
import XcLogList from './XcLogList.vue'
import xcForm from '@/views/gd/xcap/xcForm.vue'
import khList from "@/views/gd/ywd/khList.vue";
import { listYwd, getYwd, delYwd, genXcLog } from "@/api/gd/ywd";
import { getToken } from '@/utils/auth'
import { parseTime } from '@/utils/ruoyi'
export default {
  name: "Ywd",
  components: { khList, xcForm, XcLogList, FormInfo },
  data() {
    return {
      sfyxc: [],
      showUserDialog: false,
      ShowBtn: true,
      showPdf: false,
      fjTitle: "",
      fjId: "",
      uploadHeaders: { 'Authorization': 'Bearer ' + getToken() }, // 如果需要CSRF保护的话
      fjList: [],
      showUser: false,
      // 按钮loading
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业务单表格数据
      ywdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        zbbh: undefined,
        kh: undefined,
        dz: undefined,
        fj: undefined,
        fjpdf: undefined,
        xcsj: undefined,
        sfhk: undefined,
        hkje: undefined,
        hksj: undefined,
        bz: undefined,
        xcpx: true,
        params: {
          startTime: '',
          endTime: ''
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      xcData: {},
      logData: {},
      showXcLog: false,
      querySj: [],
      xcList: []
    };
  },
  created() {
    this.getList();
    // 是否已下场
    this.getDicts("gd_xczt").then(res => {
      this.xcList = res.data
    })
  },
  methods: {
    selectSfyxc(val) {
      this.queryParams.sfyxc = val.toString()
      this.getList()
    },
    selectTime(time) {
      this.queryParams.params.startTime = time[0]
      this.queryParams.params.endTime = time[1]
    },
    finalLog(row) {
      this.logData = row
      this.showXcLog = true
    },
    closeXcLog() {
      this.showXcLog = false
    },
    // 选择下场人员
    handleUserSelection(data) {
      console.log(data)
    },
    // 安排下场
    apxcUser(row) {
      this.xcData = row
      this.showUserDialog = true
    },
    closeUser() {
      this.xcData = {}
      this.showUserDialog = false
      this.getList()
    },
    copyText(data) {
      // const clipboardObj = navigator.clipboard;
      // clipboardObj.writeText(data)
      // this.$message.success("总部编号已复制！")
      const textarea = document.createElement('textarea');
      textarea.value = data;
      document.body.appendChild(textarea);
      textarea.select();
      try {
        // 尝试执行复制操作
        const success = document.execCommand('copy');
        if (success) {
          this.$message.success("总部编号已复制！")
        } else {
          this.$message.error("总部编号复制失败！")
        }
      } catch (error) {
        this.$message.error("总部编号复制失败！")
      }
      document.body.removeChild(textarea);
    },
    showInfo(row) {
      this.form = row
      this.open = true
      this.ShowBtn = false
      this.title = "业务单信息";
    },
    closeDialog() {
      this.showUser = false
    },
    selectData(row) {
      this.form.kh = row.khname
      this.form.khid = row.id
      this.form.dz = row.dz
      this.closeDialog()
    },
    selectUser() {
      this.showUser = true
    },
    /** 查询业务单列表 */
    getList() {
      this.loading = true;
      listYwd(this.queryParams, "none").then(response => {
        this.ywdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.ShowBtn = true
      this.fjList = []
      this.reset();
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        zbbh: undefined,
        kh: undefined,
        dz: undefined,
        fj: undefined,
        fjpdf: undefined,
        xcsj: undefined,
        sfyxc: "N",
        sfhk: "N",
        hkje: undefined,
        hksj: undefined,
        bz: undefined,
        createBy: this.$store.getters.name,
        createTime: parseTime(new Date()),
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    allQuery() {
      this.showQuery = true
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        zbbh: undefined,
        kh: undefined,
        dz: undefined,
        fj: undefined,
        fjpdf: undefined,
        xcsj: undefined,
        sfhk: undefined,
        hkje: undefined,
        hksj: undefined,
        bz: undefined,
        xcpx: true,
        params: {
          startTime: '',
          endTime: ''
        }
      }
      this.querySj = []
      this.sfyxc = []
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加业务单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getYwd(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改业务单";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id
      this.$modal.confirm('请确认是否删除编号为 ' + row.zbbh + ' 的数据?').then(() => {
        this.loading = true;
        return delYwd(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    // 批量删除数据
    handleQueryDelete() {
      const ids = this.ids;
      const data = this.ywdList.filter(item => ids.some(id => id === item.id)).map(item => item.zbbh)
      this.$modal.confirm('请确认是否删除编号为 ' + data + ' 的数据?').then(() => {
        this.loading = true;
        return delYwd(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      let name = this.getTodayDate()
      this.download('gd/ywd/export/xcap', {
        ...this.queryParams
      }, `${name} 下场安排_${new Date().getTime()}.xlsx`)
    },
    handleSelectExport() {
      const ids = this.ids;
      let name = this.getTodayDate()
      this.download('gd/ywd/exportSelectDat/' + ids, {
        ...this.queryParams
      }, `${name} 下场安排_${new Date().getTime()}.xlsx`)
    },
    getTodayDate() {
      const date = new Date();
      const year = date.getFullYear();
      const month = ('0' + (date.getMonth() + 1)).slice(-2);
      const day = ('0' + date.getDate()).slice(-2);
      return `${year}${month}${day}`;
    }
  }
};
</script>
